#!/bin/bash
##
# TestScript.sh
##

# EXTENSIONS  : "app"				# Accepted file extensions
# OSTYPES	  :	"APPL"				# Accepted file types
# ROLE		  : Editor				# Role (Editor, Viewer, None)

for file; do
	outputlog="$HOME/Desktop/OolitePatch.log"
	echo "`date`" >> "${outputlog}"
	echo "<- Drop Upgrade running ->" >> "${outputlog}"
	#
	# check the information about the file handed us
	#
	upgradesuccess="Failed"
	errorinfo="No error."
	#
	if [ -e "${file}/Contents/PkgInfo" >> "${outputlog}" ]
	then
		fileinfo="`cat "${file}/Contents/PkgInfo"`"
		if [ "$fileinfo" != "APPLOol8" ]
		then
			errorinfo="${file} is not Oolite."
		fi
	else
		fileinfo="Not Found"
		errorinfo="${file} is not the right sort of application."
	fi
	
	if [ -e "${file}/Contents/Info.plist" >> "${outputlog}" ]
	then
		versioninfo="`grep -A 1 'CFBundleVersion' "${file}/Contents/Info.plist" | grep 'string'`"
		versioninfo="${versioninfo##*<string>}"
		versioninfo="${versioninfo%%</string>*}"
		versionokay="NO"
		
		for item in `cat "${0%script}versions"`
		do
			if [ "$versioninfo" = "$item" ]
			then
				versionokay="YES"
			fi
		done
		
		if [ "$versionokay" = "NO" ]
		then
			errorinfo="${file} is not a suitable version of Oolite."
		fi
	else
		versioninfo="Not Found"
		errorinfo="${file}/Contents/Info.plist missing or unreadable."
	fi
	
	if [ "$errorinfo" = "No error." ]
	then
		if chmod -R +w "${file}" >> "${outputlog}"
		then
			echo "Can write into Oolite.app" >> "${outputlog}"
		else
			echo "Could not write into Oolite.app" >> "${outputlog}"
			fileinfo="Not Found"
			errorinfo="Could not change the permissions of Oolite.app."
		fi
	fi
	
	if [ "$errorinfo" = "No error." ]
	then
		if [ -e "${0%script}toc" >> "${outputlog}" ]
		then
			echo "Found table of contents at ${0%script}toc" >> "${outputlog}"
		else
			echo "Could not find table of contents at ${0%script}toc" >> "${outputlog}"
			fileinfo="Not Found"
			errorinfo="With no toc, no upgrade can proceed."
		fi
	fi
	
	if [ "$errorinfo" = "No error." ]
	then
		echo "File info is '$fileinfo'" >> "${outputlog}"
		echo "Version is '$versioninfo'" >> "${outputlog}"
		upgradesuccess="Success"
		echo "File info matches. Upgrading ..." >> "${outputlog}"
		
		#### Create new directories if needed
		if [ "$upgradesuccess" = "Success" ]
		then
			if [ -e "${0%script}newdirs" >> "${outputlog}" ]
			then
				echo "Found new directories list list at ${0%script}newdirs" >> "${outputlog}"
				
				for item in `cat "${0%script}newdirs"`
				do
					if [ "$upgradesuccess" = "Success" ]
					then
						# Only create directory if it doesn't exist...
						if [ ! -e "${file}$item" >> "${outputlog}" ]
						then
							if mkdir "${file}$item"
							then
								echo ".. created $item okay" >> "${outputlog}"
							else
								echo ".. *** failed to create $item" >> "${outputlog}"
								upgradesuccess="Failed"
							fi
						else
							# It's there; ensure it's a directory.
							if [ ! -d "${file}$item" >> "${outputlog}" ]
							then
								echo ".. *** failed to create $item -- already exists, but isn't a directory!" >> "${outputlog}"
								upgradesuccess="Failed"
							fi
						fi
					fi
				done
			fi
		fi
		
		#### now to do the actual copying of files
		if [ "$upgradesuccess" = "Success" ]
		then
			if [ -e "${0%script}toc" >> "${outputlog}" ]
			then
				echo "Found files to copy list list at ${0%script}toc" >> "${outputlog}"
				for item in `cat "${0%script}toc"`
				do
					if [ "$upgradesuccess" = "Success" ]
					then
						if [ -e "${file}$item" ]
						then
							if rm -rf "${file}$item" >> "${outputlog}"
							then
								echo ".. removed old $item okay" >> "${outputlog}"
							fi
						fi
						if cp -Rf "${0%script}Upgrade$item" "${file}$item" >> "${outputlog}"
						then
							echo ".. copied $item okay" >> "${outputlog}"
						else
							echo ".. *** failed to copy $item" >> "${outputlog}"
							upgradesuccess="Failed"
						fi
					fi
				done
			fi
		fi
		
		#### ...and the deleting of files
		if [ "$upgradesuccess" = "Success" ]
		then
			if [ -e "${0%script}delete" >> "${outputlog}" ]
			then
				echo "Found deletion list at ${0%script}delete" >> "${outputlog}"
				
				for item in `cat "${0%script}delete"`
				do
					if [ "$upgradesuccess" = "Success" ]
					then
						# only delete the file if it exists..
						if [ -e "${file}$item" >> "${outputlog}" ]
						then
							if rm -f "${file}$item" >> "${outputlog}"
							then
								echo ".. deleted $item okay" >> "${outputlog}"
							else
								echo ".. *** failed to delete $item" >> "${outputlog}"
								upgradesuccess="Failed"
							fi
						fi
					fi
				done
			fi
		fi
		
		#### write protect the file again
		chmod -R -w "${file}" >> "${outputlog}"
		
		echo "Done" >> "${outputlog}"
	else
		echo "$errorinfo No upgrade performed." >> "${outputlog}"
	fi
	echo "<- Drop Upgrade finished ->" >> "${outputlog}"
	echo "" >> "${outputlog}"
	echo "result: $upgradesuccess" >> "${outputlog}"
	echo "" >> "${outputlog}"
	
	#### if we succeeded then launch Oolite with an Updated! message
	if [ "$upgradesuccess" = "Success" ]
	then
		exec "${file}/Contents/MacOS/Oolite" -message "Successfully updated to v1.69.1.1-test." -showversion
	fi
done
